
<table id="commodity_data" style="position:relative;z-index:2"></table>

<script type="text/javascript">
	var obj;
	var editRow=undefined;//记录当前正在被编辑的行
	var currentOp;
	var flag;
	
	obj=$('#commodity_data').datagrid({
		url:'../back/commodity/select',
		fitColumns:true,
		striped:true,
		loadMsg:"数据加载中...",
		pagination:true,
		fit:true,
		pageNumber:1,
		pageSize:10,
		pageList:[10,20,30,40,50],
		sortName:'commid',
		remoteSort:false,
		columns:[[
			{field:'commids',title:'',width:100,align:'center',checkbox:true},
			{field:'commid',title:'商品编号',width:100,align:'center',sortable:true},
			{field:'cname',title:'商品名',width:100,align:'center',editor:{type:'text',option:{requires:true}}},
			{field:'cdescribe',title:'商品描述',width:100,align:'center',editor:{type:'text',option:{requires:true}}},
			{field:'cimg',title:'商品图片',width:100,align:'center',editor:{type:'text',option:{requires:true}}},
			{field:'price',title:'商品价格',width:100,align:'center',editor:{type:'text',option:{requires:true}}},
			{field:'stock',title:'库存',width:100,align:'center',editor:{type:'text',option:{requires:true}}},
			{field:'createdate',title:'创建日期',width:100,align:'center',
				formatter:function(value,row,index){  
					if(value==null){
						return '';
					}
                    var Timestamp = new Date(value);  
                    return Timestamp.toLocaleString();  
                }  
			},
			{field:'createperson',title:'创建人',width:100,align:'center'},
			{field:'updatedate',title:'修改日期',width:100,align:'center',
				formatter:function(value,row,index){  
					if(value==null){
						return '';
					}
                    var unixTimestamp = new Date(value);  
                    return unixTimestamp.toLocaleString();  
                }  	
			},
			{field:'updateperson',title:'修改人',width:100,align:'center'},
			{field:'delflag',title:'是否删除',width:100,align:'center'},
			{field:'status',title:'状态',width:100,align:'center',
				editor:{
					type:'combobox',
					options:{
						data:[{'key':'上架','value':'1'},{'key':'下架','value':'0'}],
						valueField: 'value',    
	                    textField: 'key',    
	                    panelHeight: 'auto',  
	                    required: true ,  
	                    editable:false 
					}
				},
                formatter:function(value,row,index){  
                    if(value=='1'){
                        return '上架';
                    }else{
                        return '下架'
                    }
                }
            }
		]],
		toolbar:[{
			text:"添加",
			iconCls:"icon-add",
			handler:function(){
				flag="添加";
				currentOp="insert";
				if (editRow != undefined) {
                    $("#commodity_data").datagrid('endEdit', editRow);
                }
                if (editRow == undefined) {
                    $("#commodity_data").datagrid('insertRow', {
                        index: 0,
                        row: {}
                    });
                    $("#commodity_data").datagrid('beginEdit', 0);
                    editRow = 0;
                }
			}
		},{
			text:'删除',
			iconCls:"icon-remove",
			onClick:function(){
			//获取选中的行
				var rows=obj.datagrid("getChecked");
				//alert(typeof rows[0].tid);
				if(rows[0]!=undefined){
					$.messager.confirm('信息确认', '您真的要删除选中的数据吗?', function(rs){
						if (rs){
							var commids ="";
							for(var i=0;i<rows.length-1;i++){
								commids += rows[i].commid+",";
							}
							commids += rows[i].commid;
							
							//发请求到数据库
							/* $.post("../back/address/delete",{"wbids":wbids},function(data){
								flag="删除";
								data=JSON.parse(data);
								//alert(data.code);
								if(data.code>0){
									$.messager.show({title:'成功提示',msg:data.desc,timeout:2000,showType:'slide'});
									rows=null;
									obj.datagrid("reload");//刷新表格
								}else{
									$.messager.alert('失败提示',data.desc,'error');
								}
							}); */
							$.ajax({
								  type: 'DELETE',
								  url: "../back/commodity/"+commid,//现在这个删除的方法不能删除多个，只能单个删除
								  dataType: "JSON",
								  success: function(data){
									  if(data.code=='0000'){
										  //成功
										  $.messager.show({title:'成功提示',msg:'商品标签删除成功....',timeout:2000,showType:'slide'});
										rows=null;
										obj.datagrid("reload");//刷新表格
									  }else{
										  $.messager.alert('失败提示','商品标签删除失败,请确认该类别下无新闻....','error');
									  }
								  }
							});
						}else{
							return;	
						}
					});
				}else{
					$.messager.show({title:'温馨提示',msg:'请选中您要删除的数据.....',timeout:2000,showType:'slide'});
				}
			}
		},{
			text:'保存',
			iconCls:"icon-save",
			handler:function(){
				//先关闭编辑
				obj.datagrid("endEdit",editRow);
				//获取正在被编辑的数据
				var rows=obj.datagrid("getChanges");
				//alert(rows);
				if(rows==undefined){//说明没有被保存的数据
					obj.datagrid("rejectChanges");//回滚所有数据
					obj.datagrid("endEdit",editRow);//关闭正在被编辑的行
					editRow=undefined;
				}else if(rows.cname==""){
					editRow=undefined;
					obj.datagrid("rejectChanges");
				}else{
					//保存到数据库
					//rows["op"]=currentOp;
					var commid=$(rows).attr("commid");//这里用$()将一个js对象包裹起来，将js对象转换成一个jquery对象
					//jquery对象--》js对象    ===》1、$("#div").eq(0);  2、$("#div")[0]    
					//alert(rows);
					var commid=$(rows).attr("commid");
					var cname=$(rows).attr("cname");
					var cdescribe=$(rows).attr("cdescribe");
					var cimg=$(rows).attr("cimg");
					var price=$(rows).attr("price");
					var stock=$(rows).attr("stock");
					var createdate=$(rows).attr("createdate");
					var createperson=$(rows).attr("createperson");
					var updatedate=$(rows).attr("updatedate");
					var updateperson=$(rows).attr("updateperson");
					var delflag=$(rows).attr("delflag");
					var status=$(rows).attr("status");
					$.ajax({
						  type: 'POST',
						  url: "../back/commodity/"+currentOp,
						  dataType: "JSON",
						  data: {"commid":commid,"cname":cname,"cdescribe":cdescribe,"cimg":cimg,"price":price,"stock":stock,"createdate":createdate,
							  "createperson":createperson,"updatedate":updatedate,"updateperson":updateperson,"delflag":delflag,"status":status},
						  success: function(data){
							  if(data.code=="0000"){
								  $.messager.show({
										title:'成功提示',
										msg:'商品标签'+flag+'成功....',
										timeout:2000,
										showType:'slide'
									});
									rows=null;
									editRow=undefined;
									obj.datagrid("endEdit",editRow);//关闭正在被编辑的行
									obj.datagrid("reload");//刷新表格
							  }else{
								    $.messager.alert('失败提示','商品标签'+flag+'失败....','error');
								    obj.datagrid("rejectChanges");//回滚所有数据
									rows=null;
									editRow=undefined;
									obj.datagrid("endEdit",editRow);//关闭正在被编辑的行
							  }
						 }
					});
				}
			}
		},{
			text:'撤销',
			iconCls:"icon-redo",
			handler:function(){
				obj.datagrid("rejectChanges");//回滚所有数据
				obj.datagrid("endEdit",editRow);//关闭正在被编辑的行
				editRow=undefined;	
			}
		}]
	});
	
	
</script>
